//
//  SwiftUIView.swift
//  Tiktok
//
//  Created by xmd on 11/4/20.
//

import SwiftUI
import Combine

class M : ObservableObject{
    @Published var img: UIImage?
    var cancellable: AnyCancellable?
    init() {
        cancellable?.cancel()
        cancellable = URLSession.shared.dataTaskPublisher(for: URL(string: "https://p3-dy.byteimg.com/obj/2406a00005d38ce848bc6")!)
        .map { data, urlResponse in UIImage(data: data) }
        .receive(on: DispatchQueue.main)
        .replaceError(with: nil)
            .assign(to: \.img, on: self)
    }
}

struct TestImageView: View {
    var body: some View {
        if #available(iOS 14.0, *) {
            VStack(spacing: 0) {
                Rectangle()
                    .fill(Color(#colorLiteral(red: 0.1301785111, green: 0.5873633027, blue: 0.9524403214, alpha: 1)))
                    .frame(height: 50)
                ZStack {
                    Rectangle()
                        .fill(Color(#colorLiteral(red: 0.1301785111, green: 0.5873633027, blue: 0.9524403214, alpha: 1)))
                        .frame(height: 50)
                    Text("Flutter Demo Home Page")
                        .foregroundColor(.white)
                        .font(.title2)
                }
                Spacer()
                ZStack {
                    Rectangle()
                        .fill(Color(#colorLiteral(red: 0.1301785111, green: 0.5873633027, blue: 0.9524403214, alpha: 1)))
                        .frame(width: 50, height: 50)
                        .clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
                    Text("+").foregroundColor(.white)
                }
                .shadow(radius: 10)
                .padding(20)
                .frame(maxWidth: .infinity, alignment: .trailing)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
            .ignoresSafeArea(.all)
        } else {
            // Fallback on earlier versions
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        TestImageView()
    }
}
